home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d18 / futils.arc / FSWAP.DOC < prev    next >
Text File  |  1991-04-28  |  2KB  |  80 lines

  1.  
  2. ********************************************************************
  3. ************************* FSWAP by Rex Kerr ************************
  4. ************************ Copyright (C) 1989 ************************
  5. ********************************************************************
  6.  
  7. This is a tiny unit for Turbo Pascal 5.5, written in assembly
  8. language.  Since it is written in assembler, it is both very small
  9. and very fast.
  10.  
  11. There are just 3 procedures in this unit.
  12.  
  13. ***
  14.  
  15. QSwapB(var a,b : byte);
  16.  
  17. This swaps two bytes.
  18.  
  19. ***
  20.  
  21. QSwapW(var a,b : word);
  22.  
  23. This swaps two words.
  24.  
  25. ***
  26.  
  27. QSwapV(var a,b; len : word);
  28.  
  29. This swaps the first len bytes of a and b.
  30.  
  31. ***
  32.  
  33. That's it.  No more messy procedures like this:
  34.  
  35. procedure SwapW(var a,b : word);
  36. var temp : word;
  37. begin
  38.      temp := a;
  39.      a := b;
  40.      b := temp
  41. end;
  42.  
  43. The three QSwaps are better that pascal things like this on two
  44. counts:
  45. 1) They are faster (about 50% faster with words).
  46. 2) They save stack space, as no temp is needed.
  47.  
  48. Now, it is true that with large records and arrays ( > 150 bytes)
  49. a pascal procedure that declares an entire one of the record or
  50. array is faster.  But is is very stack hungry!  Do your own
  51. benchmarks....
  52.  
  53. type longarray = array[1..1000] of byte;
  54. var abc,xyz : longarry
  55.  . . .
  56. procedure swaparray(var firstarray,secondarray : longarray);
  57. var temparray;
  58. begin
  59.      temparray := firstarray;
  60.      firstarray := secondarray;
  61.      secondarray := temparray;
  62. end;
  63.  . . .
  64. begin
  65.      swaparray(abc,xyz);           { This is faster, but it takes }
  66.                                    { 1K extra in stack space }
  67.      qswapv(abc,xyz,sizeof(abc));  { This is almost as fast, and }
  68.                                    { uses almost no stack space }
  69. end.
  70.  
  71.    The fswap procedures take advantage of an assembly language
  72. command, XCHG.  It swaps two bytes or words.  It executes about
  73. as fast as a XOR does, so it is about three times faster than
  74. the XORing trick of
  75.                        a := a xor b;
  76.                        b := a xor b;
  77.                        a := a xor b;
  78.  
  79.    Now, why doesn't someone put XCHG into Turbo Pascal?
  80.